package com.himiotech.core.framework.config;

import lombok.Data;
import org.springframework.beans.BeansException;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wuwenbin
 **/
@Data
@Configuration
@ConfigurationProperties(prefix = "himio")
public class HimioConfig implements ApplicationContextAware {

    private static ApplicationContext applicationContext;
    /**
     * 上传路径
     */
    private String profile;
    /**
     * 是否获取 ip 的地理位置
     */
    private boolean addressEnabled;
    /**
     * 项目名称
     */
    private String name;
    /**
     * 是否为演示模式
     */
    private boolean demoMode;
    private String demoIncludeUrl;
    private String demoExcludeUrl;
    /**
     * 版本
     */
    private String version;
    /**
     * 版本年份
     */
    private String copyrightYear;

    /**
     * 项目页面路径前缀
     */
    private String webappPath;

    /**
     * 项目的静态资源路径前缀
     */
    private String staticPath;

    /**
     * 启动程序时，是否重新载入数据库sql脚本（classpath下的 himio_admin.sql）
     */
    private boolean startInitSql;

    public static String getProfileStatic() {
        return applicationContext.getBean(HimioConfig.class).getProfile();
    }

    public static String getWebappPathStatic() {
        return applicationContext.getBean(HimioConfig.class).getWebappPath();
    }

    public static String getStaticPathStatic() {
        return applicationContext.getBean(HimioConfig.class).getStaticPath();
    }

    public static boolean isAddressEnabledStatic() {
        return applicationContext.getBean(HimioConfig.class).isAddressEnabled();
    }

    public static boolean isDemoModeStatic() {
        return applicationContext.getBean(HimioConfig.class).isDemoMode();
    }

    public static List<String> getDemoIncludeUrls() {
        return Arrays.stream(applicationContext.getBean(HimioConfig.class).getDemoIncludeUrl().split(",")).collect(Collectors.toList());
    }

    public static List<String> getDemoExcludeUrls() {
        return Arrays.stream(applicationContext.getBean(HimioConfig.class).getDemoExcludeUrl().split(",")).collect(Collectors.toList());
    }

    /**
     * 获取头像上传路径
     */
    public static String getAvatarPath() {
        return getUploadPath() + "/avatar";
    }

    /**
     * 获取下载路径
     */
    public static String getDownloadPath() {
        return getProfileStatic() + "/download/";
    }

    /**
     * 获取上传路径
     */
    public static String getUploadPath() {
        return getProfileStatic() + "/upload";
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        HimioConfig.applicationContext = applicationContext;
    }

}
